from selenium import webdriver
import time
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
service=EdgeService()
options=webdriver.EdgeOptions()
options.add_experimental_option('detach',True)
options.add_experimental_option('excludeSwitches',['enable-automation'])
options.add_argument("user-agent=Mozilla/5.0 \
    (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
driver=webdriver.Edge(service=service, options=options)
driver.implicitly_wait(5)
driver.get('http://aiportal.unicom.local/portal/v1/page/modules/index/index.html?_=20250407&v=1744029009291')
input('请手动进行登录，进入智慧法制，\n滑到分公司专区，点击“更多”，关闭其它标签页，只保留“智慧法制平台”标签页。手动跳转到开始的页数。后面将自动阅读点赞评论3页各10篇文章。\
    \n然后输入任意内容继续')
#driver.get('https://lawplatform.chinaunicom.cn/web/#/publicityPage/index')
handles = driver.window_handles
print("所有标签页句柄:", handles)
new_tab_handle = handles[-1]
driver.switch_to.window(new_tab_handle)
print("切换后当前标签页 URL:", driver.current_url)
for _ in range(4):#阅读页数 一页10篇
    aList=driver.find_elements(By.CLASS_NAME,'publicity-more-right-cart-list-header')
    original_handle = driver.current_window_handle
    #print('切换后句柄'+original_handle)
    print(aList)
    for a in aList:
        a.click()
        time.sleep(1)
        handles = driver.window_handles
        #print("所有标签页句柄:", handles)
        new_tab_handle = handles[-1]
        driver.switch_to.window(new_tab_handle)
        #print("当前标签页 URL:", driver.current_url)
        try:
            toLike=driver.find_elements(By.CLASS_NAME,'review-content-bottom-item')[0]
            actions = ActionChains(driver)
            actions.move_to_element(toLike).perform()
            #time.sleep(1)
            toLike.click()
            print('点赞成功')
            toReview=driver.find_element(By.CLASS_NAME,'ant-input').send_keys('已学习')
            #time.sleep(1)
            sendReview=driver.find_element(By.CLASS_NAME,'ant-btn.ant-btn-primary')
            actions.move_to_element(sendReview).perform()
            sendReview.click()
            time.sleep(1)
            print('评论成功')
        except:
            print('文章无评论区或已经点赞')
        driver.close()
        driver.switch_to.window(original_handle)
    nextPage=driver.find_element(By.CLASS_NAME,'ant-pagination-next').click()
    



